<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nyext="urn:noury-extensions">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:param name="package" />
  <xsl:param name="outdir" />
  <xsl:param name="name" />
  <xsl:param name="domainName" />
  <xsl:variable name="domain" select="/manifest/types/domain[@name=$domainName]" />

  <xsl:include href="functions.xslt" />

  <xsl:template name="generate-model-file">
  <xsl:variable name="src">
  <xsl:text>//$Id$

using System;
using System.Collections.Generic;
using System.Text;
using Salamanca;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;
using Salamanca.DataRules;

namespace </xsl:text>
          <xsl:value-of select="$package"/>
          <xsl:text>
{



    partial interface IAsk</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>QuestionFactory
    {

        /// &lt;summary&gt;Gets a question that asks for a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        Question Ask</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>(ActivityController controller);
    }



    partial interface IAsk</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>InfosQuestionFactory
    {

        /// &lt;summary&gt;Gets a question about a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        Question Ask</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Infos(ActivityController controller, DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text> domainModel);
    }



    partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>Create&lt;TActivityData&gt;
    {

        /// &lt;summary&gt;Performs the actual initialization of a new &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        private ActivityStateResult _Handle(IActivityController controller)
        {
            Data.</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>=new DomainModel.</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>(((IDataActivityParameters)Parameters).DataMappers);
            return ActivityStateResult.Next;
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        private ActivityState&lt;TActivityData&gt; _NextState
        {
            get
            {
                if (_NextStateCreator!=null)
                    return _NextStateCreator(Data, Parameters);

                return new EndActivityState&lt;TActivityData&gt;(Data);
            }
        }

        private NextStateCreateHandler&lt;TActivityData&gt; _NextStateCreator;
    }



    partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>Save&lt;TActivityData&gt;
    {

        /// &lt;summary&gt;Performs the actual saving of the &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        private ActivityStateResult _Handle(IActivityController controller)
        {
            Data.</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>.Save();
            return ActivityStateResult.Next;
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        private ActivityState&lt;TActivityData&gt; _NextState
        {
            get
            {
                if (_NextStateCreator!=null)
                    return _NextStateCreator(Data, Parameters);

                return new EndActivityState&lt;TActivityData&gt;(Data);
            }
        }

        private NextStateCreateHandler&lt;TActivityData&gt; _NextStateCreator;
    }



    partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>Delete&lt;TActivityData&gt;
    {

        /// &lt;summary&gt;Performs the actual deletion of the &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        private ActivityStateResult _Handle(IActivityController controller)
        {
            Data.</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>.Delete();
            return ActivityStateResult.Next;
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        private ActivityState&lt;TActivityData&gt; _NextState
        {
            get
            {
                if (_NextStateCreator!=null)
                    return _NextStateCreator(Data, Parameters);

                return new EndActivityState&lt;TActivityData&gt;(Data);
            }
        }

        private NextStateCreateHandler&lt;TActivityData&gt; _NextStateCreator;
    }



    partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>AskInfos&lt;TActivityData, TQuestionFactory&gt;
    {

        /// &lt;summary&gt;Raises the &lt;see cref="ActivityState{TActivityData}.Initialized" /&gt; event.&lt;/summary&gt;
        private void _OnInitialized(ActivityStateEventArgs e)
        {
            Question=((IQuestionFactoryParameters&lt;TQuestionFactory&gt;)Parameters).QuestionFactory.Ask</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>Infos((ActivityController)e.Controller, Data.</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>);
            base.OnInitialized(e);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        private ActivityState&lt;TActivityData&gt; _NextState
        {
            get
            {
                if (_NextStateCreator!=null)
                    return _NextStateCreator(Data, Parameters);

                return new EndActivityState&lt;TActivityData&gt;(Data);
            }
        }

        private NextStateCreateHandler&lt;TActivityData&gt; _NextStateCreator;
    }



    partial class </xsl:text>
    <xsl:value-of select="$domainName"/>
    <xsl:text>Ask&lt;TActivityData, TQuestionFactory&gt;
    {

        /// &lt;summary&gt;Raises the &lt;see cref="ActivityState{TActivityData}.Initialized" /&gt; event.&lt;/summary&gt;
        private void _OnInitialized(ActivityStateEventArgs e)
        {
            Question=((IQuestionFactoryParameters&lt;TQuestionFactory&gt;)Parameters).QuestionFactory.Ask</xsl:text>
            <xsl:value-of select="$domainName"/>
            <xsl:text>((ActivityController)e.Controller);
            base.OnInitialized(e);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        private ActivityState&lt;TActivityData&gt; _NextState
        {
            get
            {
                if (_NextStateCreator!=null)
                    return _NextStateCreator(Data, Parameters);

                return new EndActivityState&lt;TActivityData&gt;(Data);
            }
        }

        private NextStateCreateHandler&lt;TActivityData&gt; _NextStateCreator;
    }

}
</xsl:text>
  </xsl:variable>
  <xsl:value-of select="nyext:generateFile($src, nyext:combinePath($outdir, concat(nyext:getModelFileName($name), '.cs')))" />
</xsl:template>

  <xsl:template name="generate-user-file">
      <xsl:text>// $Id$

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Salamanca;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;
using Salamanca.DataRules;

namespace </xsl:text>
      <xsl:value-of select="$package"/>
      <xsl:text>
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Interface implemented by a class that acts as a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; selector question factory.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial interface IAsk</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>QuestionFactory
    {
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Interface implemented by a class that acts as a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; modifier question factory.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial interface IAsk</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>InfosQuestionFactory
    {
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Activity state that initializes a new &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Create&lt;TActivityData&gt;:
        ActivityState&lt;TActivityData&gt;
        where TActivityData:
            IActivityData,
            I</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Container
    {

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Create{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Create(TActivityData data, IDataActivityParameters parameters):
            this(data, parameters, null)
        {
        }

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Create{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Create(TActivityData data, IDataActivityParameters parameters, NextStateCreateHandler&lt;TActivityData&gt; nextStateCreator):
            base(data, parameters)
        {
            _NextStateCreator=nextStateCreator;
        }

        /// &lt;summary&gt;Performs the actual initialization of a new &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        protected override ActivityStateResult Handle(IActivityController controller)
        {
            return _Handle(controller);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        protected override IActivityState NextState
        {
            get
            {
                return _NextState;
            }
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Activity state that saves a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Save&lt;TActivityData&gt;:
        ActivityState&lt;TActivityData&gt;
        where TActivityData:
            IActivityData,
            I</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Container
    {

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Save{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Save(TActivityData data, IActivityParameters parameters):
            this(data, parameters, null)
        {
        }

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Save{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Save(TActivityData data, IActivityParameters parameters, NextStateCreateHandler&lt;TActivityData&gt; nextStateCreator):
            base(data, parameters)
        {
            _NextStateCreator=nextStateCreator;
        }

        /// &lt;summary&gt;Performs the actual saving of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        protected override ActivityStateResult Handle(IActivityController controller)
        {
            return _Handle(controller);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        protected override IActivityState NextState
        {
            get
            {
                return _NextState;
            }
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Activity state that deletes a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Delete&lt;TActivityData&gt;:
        ActivityState&lt;TActivityData&gt;
        where TActivityData:
            IActivityData,
            I</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Container
    {

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Delete{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Delete(TActivityData data, IActivityParameters parameters):
            this(data, parameters, null)
        {
        }

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Delete{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Delete(TActivityData data, IActivityParameters parameters, NextStateCreateHandler&lt;TActivityData&gt; nextStateCreator):
            base(data, parameters)
        {
            _NextStateCreator=nextStateCreator;
        }

        /// &lt;summary&gt;Performs the actual deletion of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
        protected override ActivityStateResult Handle(IActivityController controller)
        {
            return _Handle(controller);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        protected override IActivityState NextState
        {
            get
            {
                return _NextState;
            }
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Activity state that asks information about a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>AskInfos&lt;TActivityData, TQuestionFactory&gt;:
        QuestionActivityState&lt;TActivityData&gt;,
        IActivityInitialState
        where TActivityData:
            IActivityData,
            I</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Container
        where TQuestionFactory:
            IAsk</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>InfosQuestionFactory
    {

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>AskInfos{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>AskInfos(TActivityData data, IQuestionFactoryParameters&lt;TQuestionFactory&gt; parameters):
            this(data, parameters, null)
        {
        }

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>AskInfos{TActivityData}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>AskInfos(TActivityData data, IQuestionFactoryParameters&lt;TQuestionFactory&gt; parameters, NextStateCreateHandler&lt;TActivityData&gt; nextStateCreator):
            base(data, parameters)
        {
            _NextStateCreator=nextStateCreator;
        }

        /// &lt;summary&gt;Raises the &lt;see cref="ActivityState{TActivityData}.Initialized" /&gt; event.&lt;/summary&gt;
        protected override void OnInitialized(ActivityStateEventArgs e)
        {
            _OnInitialized(e);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        protected override IActivityState NextState
        {
            get
            {
                return _NextState;
            }
        }
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary&gt;Activity state that asks for a &lt;see cref="DomainModel.</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>" /&gt; instance.&lt;/summary&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Ask&lt;TActivityData, TQuestionFactory&gt;:
        QuestionActivityState&lt;TActivityData&gt;,
        IActivityInitialState
        where TActivityData:
            IActivityData,
            I</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Container
        where TQuestionFactory:
            IAsk</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>QuestionFactory
    {

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Ask{TActivityData, TQuestionFactory}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Ask(TActivityData data, IQuestionFactoryParameters&lt;TQuestionFactory&gt; parameters):
            this(data, parameters, null)
        {
        }

        /// &lt;summary&gt;Initializes a new instance of the &lt;see cref="</xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Ask{TActivityData, TQuestionFactory}" /&gt; class.&lt;/summary&gt;
        public </xsl:text>
      <xsl:value-of select="$domainName"/>
      <xsl:text>Ask(TActivityData data, IQuestionFactoryParameters&lt;TQuestionFactory&gt; parameters, NextStateCreateHandler&lt;TActivityData&gt; nextStateCreator):
            base(data, parameters)
        {
            _NextStateCreator=nextStateCreator;
        }

        /// &lt;summary&gt;Raises the &lt;see cref="ActivityState{TActivityData}.Initialized" /&gt; event.&lt;/summary&gt;
        protected override void OnInitialized(ActivityStateEventArgs e)
        {
            _OnInitialized(e);
        }

        /// &lt;summary&gt;Gets the next state.&lt;/summary&gt;
        protected override IActivityState NextState
        {
            get
            {
                return _NextState;
            }
        }
    }

}
</xsl:text>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="generate-model-file" />
    <xsl:call-template name="generate-user-file" />
  </xsl:template>
</xsl:stylesheet>
